#include "FWGraphic.h"
#include "FWGame.h"
#include "FWTexture.h"
#include <d3dx9.h>
FWGraphic::FWGraphic(FWGame* game)
{
	_gameObject = game;
	D3DXCreateSprite(game->d3dv9,&_d3dxSprite);
}


FWGraphic::~FWGraphic(void)
{
}

void FWGraphic::Begin()
{
	_gameObject->d3dv9->Clear(1,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(255,255,255),1,NULL);
	_gameObject->d3dv9->BeginScene();
	_d3dxSprite->Begin(D3DXSPRITE_SORT_DEPTH_BACKTOFRONT | D3DXSPRITE_ALPHABLEND );
}

void FWGraphic::Begin(D3DXMATRIX* matrix)
{
	TransformMatrix = matrix;
	Begin();
}

void FWGraphic::End()
{
	_d3dxSprite->End();
	_gameObject->d3dv9->EndScene();
	_gameObject->d3dv9->Present(NULL,NULL,NULL,NULL);
}

void FWGraphic::DrawTexture(FWTexture* texture, D3DXVECTOR3 position,D3DXVECTOR3 center, D3DCOLOR color,float depth)
{
	position.z = depth;
	_d3dxSprite->Draw(texture->getTexture(),NULL,&center,&position,color);
}

void FWGraphic::DrawTexture(FWTexture* texture,RECT rect, D3DXVECTOR2 center, D3DCOLOR color,float depth)
{

	float xScale = (float)(rect.right - rect.left) / texture->getWidth();
	float yScale = (float)(rect.bottom - rect.top) / texture->getHeight();

	D3DXMATRIX result;
	D3DXMATRIX currentState;

	D3DXMatrixScaling(&result,xScale,yScale,depth);

	D3DXVECTOR3 position;
	position.x = (float)rect.left / xScale;
	position.y = (float)rect.top / yScale;
	position.z = depth;

	D3DXVECTOR3 drawCenter;
	drawCenter.x = center.x / xScale;
	drawCenter.y = center.y / yScale;
	drawCenter.z = 0;

	_d3dxSprite->GetTransform(&currentState);
	_d3dxSprite->SetTransform(&result);
	_d3dxSprite->Draw(texture->getTexture(),NULL,&drawCenter,&position,color);
	_d3dxSprite->SetTransform(&currentState);
}

void FWGraphic::DrawTexture(FWTexture* texture,RECT srcRect,RECT destRect, D3DXVECTOR2 center, D3DCOLOR color,float depth)
{
	//tinh scale x,y
	float xScale = (float)(destRect.right - destRect.left) / (float)(srcRect.right - srcRect.left);
	float yScale = (float)(destRect.bottom - destRect.top) / (float)(srcRect.bottom - srcRect.top);

	//tao ma tran scale

	//ma tran thu phong hinh
	D3DXMATRIX result;

	//ma tran luu trang thai hien tai
	D3DXMATRIX currentState;

	//tao ma tran phong theo x,y,z
	D3DXMatrixScaling(&result,xScale,yScale,depth);

	//vi tri ve
	D3DXVECTOR3 position;
	position.x = (float)destRect.left / xScale;
	position.y = (float)destRect.top / yScale;
	position.z = depth;

	//toa do ve tai tam cua hinh
	D3DXVECTOR3 drawCenter;
	drawCenter.x = center.x / xScale;
	drawCenter.y = center.y / yScale;
	drawCenter.z = 0;

	_d3dxSprite->GetTransform(&currentState);
	_d3dxSprite->SetTransform(&result);
	_d3dxSprite->Draw(texture->getTexture(),&srcRect,&drawCenter,&position,color);
	_d3dxSprite->SetTransform(&currentState);
}


D3DXVECTOR2 FWGraphic::ConvertToGameCoordinate(D3DXVECTOR2 ObjectWorldPosition, D3DXVECTOR2 ObjectCenter)
{
	D3DXVECTOR4 result;
	D3DXVECTOR3 input;
	input.x = ObjectWorldPosition.x + ObjectCenter.x;
	input.y = ObjectWorldPosition.y + ObjectCenter.y;
	input.z = 0;
	D3DXVec3Transform(&result,&input,TransformMatrix);
	return D3DXVECTOR2(result.x,result.y);
}

void FWGraphic::tDrawTexture(FWTexture* texture, D3DXVECTOR3 position,D3DXVECTOR3 center, D3DCOLOR color, float depth)
{
	
	D3DXVECTOR2 result = ConvertToGameCoordinate(D3DXVECTOR2(position.x,position.y),D3DXVECTOR2(center.x,center.y));
	position.x = result.x;
	position.y = result.y;
	DrawTexture(texture,position,center,color,depth);
}

void FWGraphic::tDrawTexture(FWTexture* texture,RECT rect, D3DXVECTOR2 center, D3DCOLOR color,float depth)
{
	D3DXVECTOR2 result = ConvertToGameCoordinate(D3DXVECTOR2(rect.left,rect.top),D3DXVECTOR2(center.x,center.y));
	int dx = rect.right - rect.left;
	int dy = rect.bottom - rect.top;
	rect.left = result.x;
	rect.top = result.y;
	rect.right = rect.left + dx;
	rect.bottom = rect.top + dy;
	DrawTexture(texture,rect,center,color,depth);
}

void FWGraphic::tDrawTexture(FWTexture* texture,RECT srcRect,RECT destRect, D3DXVECTOR2 center, D3DCOLOR color,float depth)
{
	D3DXVECTOR2 result = ConvertToGameCoordinate(D3DXVECTOR2(destRect.left,destRect.top),D3DXVECTOR2(center.x,center.y));
	int dx = destRect.right - destRect.left;
	int dy = destRect.bottom - destRect.top;
	destRect.left = result.x;
	destRect.top = result.y;
	destRect.right = destRect.left + dx;
	destRect.bottom = destRect.top + dy;
	DrawTexture(texture,srcRect,destRect,center,color,depth);
}
